<template>
  <div class="ruler" ref="rulerRef" :style="{ display: direction === 'right' ? 'block' : 'flex' }">
    <template v-for="(s, sIdx) in size">
      <div 
        class="full-cent ruler-item" 
        :class="{ isZero: sIdx === 0, dirRight: direction === 'right' }" 
        :key="sIdx" 
        :style="{ 
          width: direction === 'up' ? rulerItemW + 'px': null,
          height: direction === 'right' ? rulerItemW + 'px': null
        }"
      >
       <span class="text">{{ sIdx * step }}</span>
      </div>
      <div 
        class="pocket-cent ruler-item" 
        :class="{ dirRight: direction === 'right' }" 
        v-for="(sm, smIdx) in 9" 
        :key="sIdx + '' + smIdx" 
        :style="{ 
           width: direction === 'up' ? rulerItemW + 'px': null,
           height: direction === 'right' ? rulerItemW + 'px': null
        }"
      >
      </div>
    </template>
  </div>
</template>

<script>
export default {
  props: {
    // 有几个大刻度
    size: {
      type: Number,
      default: 8
    },
    // 每个大刻度是多少
    step: {
      type: Number,
      default: 100
    },
    // 刻度线是在下方还是右方
    direction: {
      type: String,
      default: 'up'
    }
    
  },
  data() {
    return {
      rulerItemW: 0
    }
  },
  mounted() {
    if(this.direction == 'up') {
      this.rulerItemW = this.$refs.rulerRef.offsetWidth / 10 / this.size;
    } else if (this.direction == 'right') {
      this.rulerItemW = this.$refs.rulerRef.offsetHeight / 10 / this.size;
    }
  }
}
</script>

<style scoped>
.ruler {
  display: flex;
}
.ruler .ruler-item {
  position: relative;
  border-bottom: solid 1px gray;
}
.ruler .ruler-item.dirRight {
  border-bottom: none;
  border-top: none;
  display: flex;
  border-right: solid 1px gray;
}
.ruler .full-cent .text{
  position: relative;
  left: -12px;
}
.ruler .full-cent.dirRight .text{
  position: relative;
  left: 12px;
  top: -12px;
}
.ruler .full-cent.isZero .text {
  left: 0;
}
.ruler .full-cent.dirRight.isZero .text {
  top: 0;
  text-align: center;
  right: 10px;
  position: absolute;
}
.ruler .full-cent::before{
  content: "";
  display: block;
  width: 2px;
  height: 10px;
  background: black;
  position: absolute;
  bottom: 0;
}
.ruler .full-cent.dirRight::before {
  width: 10px;
  height: 2px;
  display: inline-block;
  position: absolute;
  right: 0;
  top: 0;
}
.ruler .pocket-cent::before {
  content: "";
  display: block;
  width: 1px;
  height: 5px;
  background: gray;
  position: absolute;
  bottom: 0;
}
.ruler .pocket-cent.dirRight::before {
  width: 5px;
  height: 1px;
  display: inline-block;
  position: absolute;
  right: 0;
  top: 0;
}
</style>